{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import warnings\n",
    "\n",
    "warnings.filterwarnings('ignore')\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "\n",
    "# !pip install plotly\n",
    "try:\n",
    "    # https://stackoverflow.com/questions/57105747/modulenotfounderror-no-module-named-plotly-graph-objects/57112843\n",
    "    #     import plotly.graph_objects as go\n",
    "    #     import plotly.express as px\n",
    "    import plotly.express as px\n",
    "    import plotly.graph_objects as go\n",
    "except ImportError as e:\n",
    "    from plotly import graph_objs as go\n",
    "    from plotly import express as px\n",
    "# import plotly.express as px\n",
    "# import plotly.graph_objects as go\n",
    "from plotly.subplots import make_subplots\n",
    "import numpy as np\n",
    "# import datetime as dt\n",
    "# from datetime import timedelta\n",
    "# from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.cluster import KMeans\n",
    "from sklearn.metrics import silhouette_score, silhouette_samples\n",
    "# from sklearn.linear_model import LinearRegression, Ridge, Lasso\n",
    "# from sklearn.svm import SVR\n",
    "# from sklearn.metrics import mean_squared_error, r2_score\n",
    "# import statsmodels.api as sm\n",
    "# from statsmodels.tsa.api import Holt, SimpleExpSmoothing, ExponentialSmoothing\n",
    "# # from fbprophet import Prophet\n",
    "# from sklearn.preprocessing import PolynomialFeatures\n",
    "# from statsmodels.tsa.stattools import adfuller\n",
    "\n",
    "# !pip install pyramid-arima\n",
    "# from pyramid.arima import auto_arima\n",
    "std = StandardScaler()\n",
    "# pd.set_option('display.float_format', lambda x: '%.6f' % x)\n",
    "# out\n",
    "# filename=r\"G:\\file\\学校\\可视化\\大作业\\COVID-19\\COVID-19-Data-master\\US\\County_level_summary\\US_County_summary_covid19_confirmed_transpose.csv\"\n",
    "\n",
    "# state_filename_base = r\"G:\\file\\学校\\可视化\\大作业\\COVID-19\\COVID-19-Data-master\\US\\State_level_summary\\US_State_summary_covid19_{}_trpo.xlsx\"\n",
    "# state_filename_base=r\"COVID-19-Data-master\\US\\State_level_summary\\US_State_summary_covid19_{}_trpo.xlsx\"\n",
    "# state_filename_base=r\"COVID-19-Data-master/US/State_level_summary/US_State_summary_covid19_{}_trpo.xlsx\"\n",
    "\n",
    "state_filename_base =r\"G:\\file\\学校\\可视化\\大作业\\COVID-19\\COVID-19-Data-master\\China\\Province_level_summary\\China_Province_summary_covid19_{}_trpo.xlsx\"\n",
    "# 支持中文\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签\n",
    "plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号\n",
    "\n",
    "# class  ClustersAnalysis:\n",
    "#     def __init__(self):\n",
    "\n",
    "def get_sum(type_name):\n",
    "    df = pd.read_excel(state_filename_base.format(type_name))\n",
    "    # pd 每一列 求和\n",
    "    df_sum = df.sum()\n",
    "    # print(\"df_sum\")\n",
    "    # print(df_sum)\n",
    "    return df_sum\n",
    "\n",
    "\n",
    "def get_3type_df():\n",
    "    # df_confirmed = pd.read_excel(state_filename_base.format(\"confirmed\"))\n",
    "    # df_recovered = pd.read_excel(state_filename_base.format(\"recovered\"))\n",
    "    # df_death = pd.read_excel(state_filename_base.format(\"death\"))\n",
    "    # df=\n",
    "\n",
    "    df = pd.DataFrame({})\n",
    "    df[\"confirmed\"] = get_sum(\"confirmed\")\n",
    "    df[\"recovered\"] = get_sum(\"recovered\")\n",
    "    df[\"death\"] = get_sum(\"death\")\n",
    "    # print(\"df_death\")\n",
    "    # print(df_death)\n",
    "    # print(\"df_death.shape\")\n",
    "    # print(df_death.shape)\n",
    "    return df\n",
    "\n",
    "\n",
    "covid = get_3type_df()\n",
    "\n",
    "confirmed_col = \"confirmed\"\n",
    "recovered_col = \"recovered\"\n",
    "death_col = \"death\"\n",
    "\n",
    "datewise = covid\n",
    "# yy=datewise[confirmed_col]-datewise[recovered_col]-datewise[death_col]\n",
    "# print(\"yy\")\n",
    "# print(yy)\n",
    "# print(\"datewise.index\")\n",
    "# print(datewise.index)\n",
    "# 总的 和\n",
    "# 根据不同的 couty\n",
    "# 确诊的 - 治愈的 - 死亡的\n",
    "# 就是现在还在患病的\n",
    "# 分配  Distribution 分布\n",
    "countrywise = datewise\n",
    "# countrywise[\"Mortality\"]=(countrywise[\"Deaths\"]/countrywise[\"Confirmed\"])*100\n",
    "# countrywise[\"Recovery\"]=(countrywise[\"Recovered\"]/countrywise[\"Confirmed\"])*100\n",
    "\n",
    "countrywise[\"Mortality\"] = (countrywise[death_col] / countrywise[confirmed_col]) * 100\n",
    "countrywise[\"Recovery\"] = (countrywise[recovered_col] / countrywise[confirmed_col]) * 100\n",
    "\n",
    "# fig=px.bar(x=datewise.index,y=datewise[confirmed_col]-datewise[recovered_col]-datewise[death_col])\n",
    "# fig.update_layout(title=\"Distribution of Number of Active Cases 累计患病的分布(各个县)\",\n",
    "#                   xaxis_title=\"县\",yaxis_title=\"Number of Cases 患病的个数\",)\n",
    "# # xaxis_title=\"Date\",yaxis_title=\"Number of Cases\",\n",
    "# fig.show()\n",
    "\n",
    "# 正在患病的分布(各个县)\"\n",
    "# 为什么没有显示呢\n",
    "\n",
    "X = countrywise[[\"Mortality\", \"Recovery\"]]\n",
    "# 死亡率 Mortality\n",
    "# Standard Scaling since K-Means Clustering is a distance based alogrithm\n",
    "# 标准缩放，因为K-均值聚类是一种基于距离的算法\n",
    "X = std.fit_transform(X)\n",
    "\n",
    "# wcss = []\n",
    "# sil = []\n",
    "# for i in range(2, 11):\n",
    "#     # 分类的个数 去尝试 每种尝试 发现 2 3 会好一些\n",
    "#     # 再根据层次聚类图 我们认为 选择分成3类比较好\n",
    "#     clf = KMeans(n_clusters=i, init='k-means++', random_state=42)\n",
    "#     clf.fit(X)\n",
    "#     labels = clf.labels_\n",
    "#     centroids = clf.cluster_centers_\n",
    "#     sil.append(silhouette_score(X, labels, metric='euclidean'))\n",
    "#     wcss.append(clf.inertia_)\n",
    "#\n",
    "\n",
    "def ElbowMethod(X):\n",
    "    wcss = []\n",
    "    sil = []\n",
    "    for i in range(2, 11):\n",
    "        # 分类的个数 去尝试 每种尝试 发现 2 3 会好一些\n",
    "        # 再根据层次聚类图 我们认为 选择分成3类比较好\n",
    "        clf = KMeans(n_clusters=i, init='k-means++', random_state=42)\n",
    "        clf.fit(X)\n",
    "        labels = clf.labels_\n",
    "        centroids = clf.cluster_centers_\n",
    "        sil.append(silhouette_score(X, labels, metric='euclidean'))\n",
    "        wcss.append(clf.inertia_)\n",
    "    x = np.arange(2, 11)\n",
    "    plt.figure(figsize=(10, 5))\n",
    "    plt.plot(x, wcss, marker='o')\n",
    "    plt.xlabel(\"Number of Clusters 集群的个数 \")\n",
    "    # 集群;群集;\n",
    "    plt.ylabel(\"Within Cluster Sum of Squares (WCSS) 簇内平方和\")\n",
    "    # 簇内平方和（WCSS）\n",
    "    plt.title(\"Elbow Method 肘部法则\")\n",
    "    # –Elbow Method和轮廓...\n",
    "    # 肘部法则\n",
    "    plt.show()\n",
    "\n",
    "\n",
    "# countrywise[\"Mortality\"]=(countrywise[\"Deaths\"]/countrywise[\"Confirmed\"])*100\n",
    "# countrywise[\"Recovery\"]=(countrywise[\"Recovered\"]/countrywise[\"Confirmed\"])*100\n",
    "\n",
    "import scipy.cluster.hierarchy as sch\n",
    "\n",
    "\n",
    "# 等级制度(尤指社会或组织); 统治集团; 层次体系; hierarchy\n",
    "#\n",
    "\n",
    "def HierarchicalClusteringTest(X):\n",
    "    plt.figure(figsize=(20, 15))\n",
    "    # dendrogram 系统树图（一种表示亲缘关系的树状图解）;\n",
    "    # 连接; 联系; 链环; 连锁; 联动装置; linkage\n",
    "\n",
    "    dendogram = sch.dendrogram(sch.linkage(X, method=\"ward\"))\n",
    "    # dendogram.\n",
    "\n",
    "    plt.show()\n",
    "\n",
    "\n",
    "def  final_KMeans(X,n_clusters):\n",
    "\n",
    "    clf_final = KMeans(n_clusters=n_clusters, init='k-means++', random_state=6)\n",
    "    # clf_final = KMeans(n_clusters=3, init='k-means++', random_state=6)\n",
    "    clf_final.fit(X)\n",
    "\n",
    "    # 分类\n",
    "    countrywise[\"Clusters\"] = clf_final.predict(X)\n",
    "\n",
    "    cluster_summary = pd.concat([countrywise[countrywise[\"Clusters\"] == 1].head(15),\n",
    "                                 countrywise[countrywise[\"Clusters\"] == 2].head(15),\n",
    "                                 countrywise[countrywise[\"Clusters\"] == 0].head(15)])\n",
    "    cluster_summary.style.background_gradient(cmap='Reds').format(\"{:.2f}\")\n",
    "    # 背景梯度\n",
    "    # plt.show()\n",
    "    cluster_summary\n",
    "    # print(\"cluster_summary\")\n",
    "    # print(cluster_summary)\n",
    "    # 数据显示  治愈率是0 这是\n",
    "    # 我们把这些州 按照治愈率和 死亡率 分成了三类\n",
    "    # 根据背景梯度图 显示 一类是死亡率较高 0-死亡率其次 , 2-死亡率最低\n",
    "\n",
    "\n",
    "# clf_final = KMeans(n_clusters=3, init='k-means++', random_state=6)\n",
    "# clf_final.fit(X)\n",
    "#\n",
    "# # 分类\n",
    "# countrywise[\"Clusters\"] = clf_final.predict(X)\n",
    "#\n",
    "# cluster_summary = pd.concat([countrywise[countrywise[\"Clusters\"] == 1].head(15),\n",
    "#                              countrywise[countrywise[\"Clusters\"] == 2].head(15),\n",
    "#                              countrywise[countrywise[\"Clusters\"] == 0].head(15)])\n",
    "# cluster_summary.style.background_gradient(cmap='Reds').format(\"{:.2f}\")\n",
    "# # 背景梯度\n",
    "# # plt.show()\n",
    "# print(\"cluster_summary\")\n",
    "# print(cluster_summary)\n",
    "# 数据显示  治愈率是0 这是\n",
    "# 我们把这些州 按照治愈率和 死亡率 分成了三类\n",
    "# 根据背景梯度图 显示 一类是死亡率较高 0-死亡率其次 , 2-死亡率最低\n",
    "\n",
    "# 显示分成三类 好一点\n",
    "# ElbowMethod(X)\n",
    "# 显示分成 3类\n",
    "# HierarchicalClusteringTest(X)\n",
    "\n",
    "final_KMeans(X,3)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x26ed55daca0>"
      ],
      "text/html": "<style type=\"text/css\">\n#T_94807_row0_col0, #T_94807_row0_col1, #T_94807_row0_col2, #T_94807_row1_col0, #T_94807_row1_col1, #T_94807_row1_col2, #T_94807_row3_col2, #T_94807_row3_col5, #T_94807_row4_col2, #T_94807_row4_col5, #T_94807_row5_col2, #T_94807_row5_col5, #T_94807_row6_col2, #T_94807_row6_col4, #T_94807_row6_col5, #T_94807_row7_col0, #T_94807_row7_col1, #T_94807_row7_col2, #T_94807_row7_col5, #T_94807_row8_col2, #T_94807_row8_col5, #T_94807_row9_col2, #T_94807_row9_col5, #T_94807_row10_col0, #T_94807_row10_col1, #T_94807_row10_col2, #T_94807_row10_col5, #T_94807_row11_col2, #T_94807_row11_col5, #T_94807_row12_col2, #T_94807_row12_col5, #T_94807_row13_col5, #T_94807_row14_col5, #T_94807_row15_col2, #T_94807_row15_col5, #T_94807_row16_col2, #T_94807_row16_col5, #T_94807_row17_col2, #T_94807_row17_col3, #T_94807_row17_col5 {\n  background-color: #fff5f0;\n  color: #000000;\n}\n#T_94807_row0_col3 {\n  background-color: #fedfd0;\n  color: #000000;\n}\n#T_94807_row0_col4, #T_94807_row1_col5, #T_94807_row2_col0, #T_94807_row2_col1, #T_94807_row2_col2, #T_94807_row2_col3, #T_94807_row2_col5 {\n  background-color: #67000d;\n  color: #f1f1f1;\n}\n#T_94807_row0_col5 {\n  background-color: #fb694a;\n  color: #f1f1f1;\n}\n#T_94807_row1_col3 {\n  background-color: #f96044;\n  color: #f1f1f1;\n}\n#T_94807_row1_col4, #T_94807_row7_col4, #T_94807_row15_col3 {\n  background-color: #ffebe2;\n  color: #000000;\n}\n#T_94807_row2_col4, #T_94807_row4_col4, #T_94807_row16_col3 {\n  background-color: #ffede5;\n  color: #000000;\n}\n#T_94807_row3_col0, #T_94807_row4_col0, #T_94807_row4_col1, #T_94807_row11_col0, #T_94807_row11_col4, #T_94807_row14_col4, #T_94807_row15_col0, #T_94807_row17_col0 {\n  background-color: #fff3ed;\n  color: #000000;\n}\n#T_94807_row3_col1, #T_94807_row12_col0, #T_94807_row12_col1, #T_94807_row15_col1 {\n  background-color: #fff2ec;\n  color: #000000;\n}\n#T_94807_row3_col3 {\n  background-color: #fee6da;\n  color: #000000;\n}\n#T_94807_row3_col4 {\n  background-color: #fee8dd;\n  color: #000000;\n}\n#T_94807_row4_col3 {\n  background-color: #fed9c9;\n  color: #000000;\n}\n#T_94807_row5_col0, #T_94807_row5_col1, #T_94807_row6_col0, #T_94807_row11_col1, #T_94807_row17_col1 {\n  background-color: #fff4ee;\n  color: #000000;\n}\n#T_94807_row5_col3 {\n  background-color: #fdd7c6;\n  color: #000000;\n}\n#T_94807_row5_col4, #T_94807_row15_col4 {\n  background-color: #fee9df;\n  color: #000000;\n}\n#T_94807_row6_col1, #T_94807_row9_col0, #T_94807_row9_col1, #T_94807_row13_col2, #T_94807_row16_col0, #T_94807_row16_col1, #T_94807_row17_col4 {\n  background-color: #fff4ef;\n  color: #000000;\n}\n#T_94807_row6_col3, #T_94807_row8_col1, #T_94807_row14_col2 {\n  background-color: #fff0e9;\n  color: #000000;\n}\n#T_94807_row7_col3 {\n  background-color: #fdd1be;\n  color: #000000;\n}\n#T_94807_row8_col0 {\n  background-color: #fff0e8;\n  color: #000000;\n}\n#T_94807_row8_col3 {\n  background-color: #ffece3;\n  color: #000000;\n}\n#T_94807_row8_col4 {\n  background-color: #ffefe8;\n  color: #000000;\n}\n#T_94807_row9_col3 {\n  background-color: #fee2d5;\n  color: #000000;\n}\n#T_94807_row9_col4, #T_94807_row13_col4 {\n  background-color: #feeae1;\n  color: #000000;\n}\n#T_94807_row10_col3 {\n  background-color: #fdc6b0;\n  color: #000000;\n}\n#T_94807_row10_col4 {\n  background-color: #fee8de;\n  color: #000000;\n}\n#T_94807_row11_col3 {\n  background-color: #fee0d2;\n  color: #000000;\n}\n#T_94807_row12_col3 {\n  background-color: #fdd3c1;\n  color: #000000;\n}\n#T_94807_row12_col4, #T_94807_row16_col4 {\n  background-color: #ffeee7;\n  color: #000000;\n}\n#T_94807_row13_col0, #T_94807_row13_col1 {\n  background-color: #fff2eb;\n  color: #000000;\n}\n#T_94807_row13_col3 {\n  background-color: #fcb99f;\n  color: #000000;\n}\n#T_94807_row14_col0 {\n  background-color: #fee3d7;\n  color: #000000;\n}\n#T_94807_row14_col1 {\n  background-color: #fee5d9;\n  color: #000000;\n}\n#T_94807_row14_col3 {\n  background-color: #fcb499;\n  color: #000000;\n}\n</style>\n<table id=\"T_94807_\">\n  <thead>\n    <tr>\n      <th class=\"blank level0\" >&nbsp;</th>\n      <th class=\"col_heading level0 col0\" >confirmed</th>\n      <th class=\"col_heading level0 col1\" >recovered</th>\n      <th class=\"col_heading level0 col2\" >death</th>\n      <th class=\"col_heading level0 col3\" >Mortality</th>\n      <th class=\"col_heading level0 col4\" >Recovery</th>\n      <th class=\"col_heading level0 col5\" >Clusters</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th id=\"T_94807_level0_row0\" class=\"row_heading level0 row0\" >Unknown</th>\n      <td id=\"T_94807_row0_col0\" class=\"data row0 col0\" >1078.00</td>\n      <td id=\"T_94807_row0_col1\" class=\"data row0 col1\" >2731.00</td>\n      <td id=\"T_94807_row0_col2\" class=\"data row0 col2\" >9.00</td>\n      <td id=\"T_94807_row0_col3\" class=\"data row0 col3\" >0.83</td>\n      <td id=\"T_94807_row0_col4\" class=\"data row0 col4\" >253.34</td>\n      <td id=\"T_94807_row0_col5\" class=\"data row0 col5\" >1.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_94807_level0_row1\" class=\"row_heading level0 row1\" >Hainan</th>\n      <td id=\"T_94807_row1_col0\" class=\"data row1 col0\" >112860.00</td>\n      <td id=\"T_94807_row1_col1\" class=\"data row1 col1\" >88741.00</td>\n      <td id=\"T_94807_row1_col2\" class=\"data row1 col2\" >3789.00</td>\n      <td id=\"T_94807_row1_col3\" class=\"data row1 col3\" >3.36</td>\n      <td id=\"T_94807_row1_col4\" class=\"data row1 col4\" >78.63</td>\n      <td id=\"T_94807_row1_col5\" class=\"data row1 col5\" >2.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_94807_level0_row2\" class=\"row_heading level0 row2\" >Hubei</th>\n      <td id=\"T_94807_row2_col0\" class=\"data row2 col0\" >43243744.00</td>\n      <td id=\"T_94807_row2_col1\" class=\"data row2 col1\" >33124012.00</td>\n      <td id=\"T_94807_row2_col2\" class=\"data row2 col2\" >2754515.00</td>\n      <td id=\"T_94807_row2_col3\" class=\"data row2 col3\" >6.37</td>\n      <td id=\"T_94807_row2_col4\" class=\"data row2 col4\" >76.60</td>\n      <td id=\"T_94807_row2_col5\" class=\"data row2 col5\" >2.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_94807_level0_row3\" class=\"row_heading level0 row3\" >Anhui</th>\n      <td id=\"T_94807_row3_col0\" class=\"data row3 col0\" >637517.00</td>\n      <td id=\"T_94807_row3_col1\" class=\"data row3 col1\" >524556.00</td>\n      <td id=\"T_94807_row3_col2\" class=\"data row3 col2\" >3803.00</td>\n      <td id=\"T_94807_row3_col3\" class=\"data row3 col3\" >0.60</td>\n      <td id=\"T_94807_row3_col4\" class=\"data row3 col4\" >82.28</td>\n      <td id=\"T_94807_row3_col5\" class=\"data row3 col5\" >0.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_94807_level0_row4\" class=\"row_heading level0 row4\" >Beijing</th>\n      <td id=\"T_94807_row4_col0\" class=\"data row4 col0\" >588739.00</td>\n      <td id=\"T_94807_row4_col1\" class=\"data row4 col1\" >451555.00</td>\n      <td id=\"T_94807_row4_col2\" class=\"data row4 col2\" >5577.00</td>\n      <td id=\"T_94807_row4_col3\" class=\"data row4 col3\" >0.95</td>\n      <td id=\"T_94807_row4_col4\" class=\"data row4 col4\" >76.70</td>\n      <td id=\"T_94807_row4_col5\" class=\"data row4 col5\" >0.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_94807_level0_row5\" class=\"row_heading level0 row5\" >Chongqing</th>\n      <td id=\"T_94807_row5_col0\" class=\"data row5 col0\" >378384.00</td>\n      <td id=\"T_94807_row5_col1\" class=\"data row5 col1\" >307823.00</td>\n      <td id=\"T_94807_row5_col2\" class=\"data row5 col2\" >3809.00</td>\n      <td id=\"T_94807_row5_col3\" class=\"data row5 col3\" >1.01</td>\n      <td id=\"T_94807_row5_col4\" class=\"data row5 col4\" >81.35</td>\n      <td id=\"T_94807_row5_col5\" class=\"data row5 col5\" >0.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_94807_level0_row6\" class=\"row_heading level0 row6\" >Fujian</th>\n      <td id=\"T_94807_row6_col0\" class=\"data row6 col0\" >355425.00</td>\n      <td id=\"T_94807_row6_col1\" class=\"data row6 col1\" >240662.00</td>\n      <td id=\"T_94807_row6_col2\" class=\"data row6 col2\" >625.00</td>\n      <td id=\"T_94807_row6_col3\" class=\"data row6 col3\" >0.18</td>\n      <td id=\"T_94807_row6_col4\" class=\"data row6 col4\" >67.71</td>\n      <td id=\"T_94807_row6_col5\" class=\"data row6 col5\" >0.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_94807_level0_row7\" class=\"row_heading level0 row7\" >Gansu</th>\n      <td id=\"T_94807_row7_col0\" class=\"data row7 col0\" >112355.00</td>\n      <td id=\"T_94807_row7_col1\" class=\"data row7 col1\" >88658.00</td>\n      <td id=\"T_94807_row7_col2\" class=\"data row7 col2\" >1273.00</td>\n      <td id=\"T_94807_row7_col3\" class=\"data row7 col3\" >1.13</td>\n      <td id=\"T_94807_row7_col4\" class=\"data row7 col4\" >78.91</td>\n      <td id=\"T_94807_row7_col5\" class=\"data row7 col5\" >0.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_94807_level0_row8\" class=\"row_heading level0 row8\" >Guangdong</th>\n      <td id=\"T_94807_row8_col0\" class=\"data row8 col0\" >1367847.00</td>\n      <td id=\"T_94807_row8_col1\" class=\"data row8 col1\" >1024876.00</td>\n      <td id=\"T_94807_row8_col2\" class=\"data row8 col2\" >5001.00</td>\n      <td id=\"T_94807_row8_col3\" class=\"data row8 col3\" >0.37</td>\n      <td id=\"T_94807_row8_col4\" class=\"data row8 col4\" >74.93</td>\n      <td id=\"T_94807_row8_col5\" class=\"data row8 col5\" >0.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_94807_level0_row9\" class=\"row_heading level0 row9\" >Guangxi</th>\n      <td id=\"T_94807_row9_col0\" class=\"data row9 col0\" >171744.00</td>\n      <td id=\"T_94807_row9_col1\" class=\"data row9 col1\" >136808.00</td>\n      <td id=\"T_94807_row9_col2\" class=\"data row9 col2\" >1268.00</td>\n      <td id=\"T_94807_row9_col3\" class=\"data row9 col3\" >0.74</td>\n      <td id=\"T_94807_row9_col4\" class=\"data row9 col4\" >79.66</td>\n      <td id=\"T_94807_row9_col5\" class=\"data row9 col5\" >0.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_94807_level0_row10\" class=\"row_heading level0 row10\" >Guizhou</th>\n      <td id=\"T_94807_row10_col0\" class=\"data row10 col0\" >93998.00</td>\n      <td id=\"T_94807_row10_col1\" class=\"data row10 col1\" >76757.00</td>\n      <td id=\"T_94807_row10_col2\" class=\"data row10 col2\" >1267.00</td>\n      <td id=\"T_94807_row10_col3\" class=\"data row10 col3\" >1.35</td>\n      <td id=\"T_94807_row10_col4\" class=\"data row10 col4\" >81.66</td>\n      <td id=\"T_94807_row10_col5\" class=\"data row10 col5\" >0.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_94807_level0_row11\" class=\"row_heading level0 row11\" >Hebei</th>\n      <td id=\"T_94807_row11_col0\" class=\"data row11 col0\" >509851.00</td>\n      <td id=\"T_94807_row11_col1\" class=\"data row11 col1\" >359271.00</td>\n      <td id=\"T_94807_row11_col2\" class=\"data row11 col2\" >4093.00</td>\n      <td id=\"T_94807_row11_col3\" class=\"data row11 col3\" >0.80</td>\n      <td id=\"T_94807_row11_col4\" class=\"data row11 col4\" >70.47</td>\n      <td id=\"T_94807_row11_col5\" class=\"data row11 col5\" >0.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_94807_level0_row12\" class=\"row_heading level0 row12\" >Heilongjiang</th>\n      <td id=\"T_94807_row12_col0\" class=\"data row12 col0\" >771143.00</td>\n      <td id=\"T_94807_row12_col1\" class=\"data row12 col1\" >579102.00</td>\n      <td id=\"T_94807_row12_col2\" class=\"data row12 col2\" >8254.00</td>\n      <td id=\"T_94807_row12_col3\" class=\"data row12 col3\" >1.07</td>\n      <td id=\"T_94807_row12_col4\" class=\"data row12 col4\" >75.10</td>\n      <td id=\"T_94807_row12_col5\" class=\"data row12 col5\" >0.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_94807_level0_row13\" class=\"row_heading level0 row13\" >Henan</th>\n      <td id=\"T_94807_row13_col0\" class=\"data row13 col0\" >849859.00</td>\n      <td id=\"T_94807_row13_col1\" class=\"data row13 col1\" >675238.00</td>\n      <td id=\"T_94807_row13_col2\" class=\"data row13 col2\" >13880.00</td>\n      <td id=\"T_94807_row13_col3\" class=\"data row13 col3\" >1.63</td>\n      <td id=\"T_94807_row13_col4\" class=\"data row13 col4\" >79.45</td>\n      <td id=\"T_94807_row13_col5\" class=\"data row13 col5\" >0.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_94807_level0_row14\" class=\"row_heading level0 row14\" >Hong Kong</th>\n      <td id=\"T_94807_row14_col0\" class=\"data row14 col0\" >4587618.00</td>\n      <td id=\"T_94807_row14_col1\" class=\"data row14 col1\" >3213515.00</td>\n      <td id=\"T_94807_row14_col2\" class=\"data row14 col2\" >79233.00</td>\n      <td id=\"T_94807_row14_col3\" class=\"data row14 col3\" >1.73</td>\n      <td id=\"T_94807_row14_col4\" class=\"data row14 col4\" >70.05</td>\n      <td id=\"T_94807_row14_col5\" class=\"data row14 col5\" >0.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_94807_level0_row15\" class=\"row_heading level0 row15\" >Hunan</th>\n      <td id=\"T_94807_row15_col0\" class=\"data row15 col0\" >674506.00</td>\n      <td id=\"T_94807_row15_col1\" class=\"data row15 col1\" >545312.00</td>\n      <td id=\"T_94807_row15_col2\" class=\"data row15 col2\" >2526.00</td>\n      <td id=\"T_94807_row15_col3\" class=\"data row15 col3\" >0.37</td>\n      <td id=\"T_94807_row15_col4\" class=\"data row15 col4\" >80.85</td>\n      <td id=\"T_94807_row15_col5\" class=\"data row15 col5\" >0.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_94807_level0_row16\" class=\"row_heading level0 row16\" >Inner Mongolia</th>\n      <td id=\"T_94807_row16_col0\" class=\"data row16 col0\" >198128.00</td>\n      <td id=\"T_94807_row16_col1\" class=\"data row16 col1\" >148617.00</td>\n      <td id=\"T_94807_row16_col2\" class=\"data row16 col2\" >613.00</td>\n      <td id=\"T_94807_row16_col3\" class=\"data row16 col3\" >0.31</td>\n      <td id=\"T_94807_row16_col4\" class=\"data row16 col4\" >75.01</td>\n      <td id=\"T_94807_row16_col5\" class=\"data row16 col5\" >0.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_94807_level0_row17\" class=\"row_heading level0 row17\" >Jiangsu</th>\n      <td id=\"T_94807_row17_col0\" class=\"data row17 col0\" >522012.00</td>\n      <td id=\"T_94807_row17_col1\" class=\"data row17 col1\" >360083.00</td>\n      <td id=\"T_94807_row17_col2\" class=\"data row17 col2\" >0.00</td>\n      <td id=\"T_94807_row17_col3\" class=\"data row17 col3\" >0.00</td>\n      <td id=\"T_94807_row17_col4\" class=\"data row17 col4\" >68.98</td>\n      <td id=\"T_94807_row17_col5\" class=\"data row17 col5\" >0.00</td>\n    </tr>\n  </tbody>\n</table>\n"
     },
     "metadata": {},
     "execution_count": 3
    }
   ],
   "source": [
    "clf_final = KMeans(n_clusters=3, init='k-means++', random_state=6)\n",
    "    # clf_final = KMeans(n_clusters=3, init='k-means++', random_state=6)\n",
    "clf_final.fit(X)\n",
    "\n",
    "# 分类\n",
    "countrywise[\"Clusters\"] = clf_final.predict(X)\n",
    "\n",
    "cluster_summary = pd.concat([countrywise[countrywise[\"Clusters\"] == 1].head(15),\n",
    "                                countrywise[countrywise[\"Clusters\"] == 2].head(15),\n",
    "                                countrywise[countrywise[\"Clusters\"] == 0].head(15)])\n",
    "cluster_summary.style.background_gradient(cmap='Reds').format(\"{:.2f}\")\n",
    "# 背景梯度\n",
    "# plt.show()\n",
    "# cluster_summary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "output_type": "error",
     "ename": "KeyError",
     "evalue": "\"['Unknown'] not found in axis\"",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_29148/3656202892.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# countrywise.drop(index=(countrywise.loc[(countrywise['table']=='sc')].index))\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;31m# countrywise.drop(index=\"Unknown\",replace=True)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mcountrywise\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mcountrywise\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdrop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m\"Unknown\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m \u001b[1;31m# print(\"countrywise.index\")\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;31m# print(countrywise.index)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\software\\anaconda\\lib\\site-packages\\pandas\\util\\_decorators.py\u001b[0m in \u001b[0;36mwrapper\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m    309\u001b[0m                     \u001b[0mstacklevel\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mstacklevel\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    310\u001b[0m                 )\n\u001b[1;32m--> 311\u001b[1;33m             \u001b[1;32mreturn\u001b[0m \u001b[0mfunc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    312\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    313\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mwrapper\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\software\\anaconda\\lib\\site-packages\\pandas\\core\\frame.py\u001b[0m in \u001b[0;36mdrop\u001b[1;34m(self, labels, axis, index, columns, level, inplace, errors)\u001b[0m\n\u001b[0;32m   4904\u001b[0m                 \u001b[0mweight\u001b[0m  \u001b[1;36m1.0\u001b[0m     \u001b[1;36m0.8\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   4905\u001b[0m         \"\"\"\n\u001b[1;32m-> 4906\u001b[1;33m         return super().drop(\n\u001b[0m\u001b[0;32m   4907\u001b[0m             \u001b[0mlabels\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mlabels\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   4908\u001b[0m             \u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\software\\anaconda\\lib\\site-packages\\pandas\\core\\generic.py\u001b[0m in \u001b[0;36mdrop\u001b[1;34m(self, labels, axis, index, columns, level, inplace, errors)\u001b[0m\n\u001b[0;32m   4148\u001b[0m         \u001b[1;32mfor\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlabels\u001b[0m \u001b[1;32min\u001b[0m \u001b[0maxes\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mitems\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   4149\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mlabels\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 4150\u001b[1;33m                 \u001b[0mobj\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mobj\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_drop_axis\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlabels\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlevel\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mlevel\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0merrors\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0merrors\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   4151\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   4152\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0minplace\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\software\\anaconda\\lib\\site-packages\\pandas\\core\\generic.py\u001b[0m in \u001b[0;36m_drop_axis\u001b[1;34m(self, labels, axis, level, errors)\u001b[0m\n\u001b[0;32m   4183\u001b[0m                 \u001b[0mnew_axis\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdrop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlabels\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlevel\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mlevel\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0merrors\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0merrors\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   4184\u001b[0m             \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 4185\u001b[1;33m                 \u001b[0mnew_axis\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdrop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlabels\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0merrors\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0merrors\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   4186\u001b[0m             \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m**\u001b[0m\u001b[1;33m{\u001b[0m\u001b[0maxis_name\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mnew_axis\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   4187\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\software\\anaconda\\lib\\site-packages\\pandas\\core\\indexes\\base.py\u001b[0m in \u001b[0;36mdrop\u001b[1;34m(self, labels, errors)\u001b[0m\n\u001b[0;32m   6015\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mmask\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0many\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   6016\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0merrors\u001b[0m \u001b[1;33m!=\u001b[0m \u001b[1;34m\"ignore\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 6017\u001b[1;33m                 \u001b[1;32mraise\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34mf\"{labels[mask]} not found in axis\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   6018\u001b[0m             \u001b[0mindexer\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mindexer\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m~\u001b[0m\u001b[0mmask\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   6019\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdelete\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mindexer\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: \"['Unknown'] not found in axis\""
     ]
    }
   ],
   "source": [
    "\n",
    "# countrywise.drop(index=(countrywise.loc[(countrywise['table']=='sc')].index))\n",
    "# countrywise.drop(index=\"Unknown\",replace=True)\n",
    "countrywise=countrywise.drop(index=\"Unknown\")\n",
    "# print(\"countrywise.index\")\n",
    "# print(countrywise.index)\n",
    "# print(\"countrywise\")\n",
    "# print(countrywise)\n",
    "# countrywise[]\n",
    "# wcss = []\n",
    "# sil = []\n",
    "# for i in range(2, 11):\n",
    "#     # 分类的个数 去尝试 每种尝试 发现 2 3 会好一些\n",
    "#     # 再根据层次聚类图 我们认为 选择分成3类比较好\n",
    "#     clf = KMeans(n_clusters=i, init='k-means++', random_state=42)\n",
    "#     clf.fit(X)\n",
    "#     labels = clf.labels_\n",
    "#     centroids = clf.cluster_centers_\n",
    "#     sil.append(silhouette_score(X, labels, metric='euclidean'))\n",
    "#     wcss.append(clf.inertia_)\n",
    "#\n",
    "\n",
    "X = countrywise[[\"Mortality\", \"Recovery\"]]\n",
    "# 死亡率 Mortality\n",
    "# Standard Scaling since K-Means Clustering is a distance based alogrithm\n",
    "# 标准缩放，因为K-均值聚类是一种基于距离的算法\n",
    "X = std.fit_transform(X)\n",
    "# pd 删掉某一行数据\n",
    "\n",
    "clf_final = KMeans(n_clusters=3, init='k-means++', random_state=6)\n",
    "# clf_final = KMeans(n_clusters=3, init='k-means++', random_state=6)\n",
    "clf_final.fit(X)\n",
    "\n",
    "# 分类\n",
    "countrywise[\"Clusters\"] = clf_final.predict(X)\n",
    "\n",
    "cluster_summary = pd.concat([countrywise[countrywise[\"Clusters\"] == 1].head(15),\n",
    "                            countrywise[countrywise[\"Clusters\"] == 2].head(15),\n",
    "                            countrywise[countrywise[\"Clusters\"] == 0].head(15)])\n",
    "cluster_summary.style.background_gradient(cmap='Reds').format(\"{:.2f}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x26edc61f6a0>"
      ],
      "text/html": "<style type=\"text/css\">\n#T_ec2a2_row0_col0, #T_ec2a2_row0_col1, #T_ec2a2_row0_col2, #T_ec2a2_row2_col1, #T_ec2a2_row2_col2, #T_ec2a2_row3_col0, #T_ec2a2_row3_col1, #T_ec2a2_row3_col2, #T_ec2a2_row3_col4, #T_ec2a2_row4_col2, #T_ec2a2_row4_col5, #T_ec2a2_row5_col2, #T_ec2a2_row5_col5, #T_ec2a2_row6_col2, #T_ec2a2_row6_col5, #T_ec2a2_row7_col2, #T_ec2a2_row7_col5, #T_ec2a2_row8_col0, #T_ec2a2_row8_col1, #T_ec2a2_row8_col2, #T_ec2a2_row8_col5, #T_ec2a2_row9_col2, #T_ec2a2_row9_col5, #T_ec2a2_row10_col0, #T_ec2a2_row10_col2, #T_ec2a2_row10_col5, #T_ec2a2_row11_col0, #T_ec2a2_row11_col1, #T_ec2a2_row11_col2, #T_ec2a2_row11_col5, #T_ec2a2_row12_col2, #T_ec2a2_row12_col5, #T_ec2a2_row13_col2, #T_ec2a2_row13_col5, #T_ec2a2_row14_col5, #T_ec2a2_row15_col5, #T_ec2a2_row16_col2, #T_ec2a2_row16_col5, #T_ec2a2_row17_col2, #T_ec2a2_row17_col5, #T_ec2a2_row18_col2, #T_ec2a2_row18_col3, #T_ec2a2_row18_col5 {\n  background-color: #fff5f0;\n  color: #000000;\n}\n#T_ec2a2_row0_col3 {\n  background-color: #f96044;\n  color: #f1f1f1;\n}\n#T_ec2a2_row0_col4 {\n  background-color: #820711;\n  color: #f1f1f1;\n}\n#T_ec2a2_row0_col5, #T_ec2a2_row1_col5 {\n  background-color: #fb694a;\n  color: #f1f1f1;\n}\n#T_ec2a2_row1_col0, #T_ec2a2_row1_col1, #T_ec2a2_row1_col2, #T_ec2a2_row1_col3, #T_ec2a2_row2_col5, #T_ec2a2_row3_col5, #T_ec2a2_row4_col4 {\n  background-color: #67000d;\n  color: #f1f1f1;\n}\n#T_ec2a2_row1_col4, #T_ec2a2_row5_col4 {\n  background-color: #900a12;\n  color: #f1f1f1;\n}\n#T_ec2a2_row2_col0, #T_ec2a2_row7_col1, #T_ec2a2_row10_col1, #T_ec2a2_row14_col2, #T_ec2a2_row17_col0, #T_ec2a2_row17_col1 {\n  background-color: #fff4ef;\n  color: #000000;\n}\n#T_ec2a2_row2_col3 {\n  background-color: #fee8de;\n  color: #000000;\n}\n#T_ec2a2_row2_col4 {\n  background-color: #fb7555;\n  color: #f1f1f1;\n}\n#T_ec2a2_row3_col3 {\n  background-color: #fcc4ad;\n  color: #000000;\n}\n#T_ec2a2_row4_col0, #T_ec2a2_row5_col0, #T_ec2a2_row5_col1, #T_ec2a2_row16_col0, #T_ec2a2_row18_col0 {\n  background-color: #fff3ed;\n  color: #000000;\n}\n#T_ec2a2_row4_col1, #T_ec2a2_row13_col0, #T_ec2a2_row13_col1, #T_ec2a2_row14_col0, #T_ec2a2_row16_col1 {\n  background-color: #fff2ec;\n  color: #000000;\n}\n#T_ec2a2_row4_col3 {\n  background-color: #fee6da;\n  color: #000000;\n}\n#T_ec2a2_row5_col3 {\n  background-color: #fed9c9;\n  color: #000000;\n}\n#T_ec2a2_row6_col0, #T_ec2a2_row6_col1, #T_ec2a2_row7_col0, #T_ec2a2_row12_col0, #T_ec2a2_row12_col1, #T_ec2a2_row18_col1 {\n  background-color: #fff4ee;\n  color: #000000;\n}\n#T_ec2a2_row6_col3 {\n  background-color: #fdd7c6;\n  color: #000000;\n}\n#T_ec2a2_row6_col4 {\n  background-color: #6d010e;\n  color: #f1f1f1;\n}\n#T_ec2a2_row7_col3, #T_ec2a2_row9_col1, #T_ec2a2_row15_col2 {\n  background-color: #fff0e9;\n  color: #000000;\n}\n#T_ec2a2_row7_col4 {\n  background-color: #c2161b;\n  color: #f1f1f1;\n}\n#T_ec2a2_row8_col3 {\n  background-color: #fdd1be;\n  color: #000000;\n}\n#T_ec2a2_row8_col4 {\n  background-color: #800610;\n  color: #f1f1f1;\n}\n#T_ec2a2_row9_col0 {\n  background-color: #fff0e8;\n  color: #000000;\n}\n#T_ec2a2_row9_col3 {\n  background-color: #ffece3;\n  color: #000000;\n}\n#T_ec2a2_row9_col4, #T_ec2a2_row17_col4 {\n  background-color: #9d0d14;\n  color: #f1f1f1;\n}\n#T_ec2a2_row10_col3 {\n  background-color: #fee2d5;\n  color: #000000;\n}\n#T_ec2a2_row10_col4, #T_ec2a2_row14_col4 {\n  background-color: #7a0510;\n  color: #f1f1f1;\n}\n#T_ec2a2_row11_col3 {\n  background-color: #fdc6b0;\n  color: #000000;\n}\n#T_ec2a2_row11_col4 {\n  background-color: #6b010e;\n  color: #f1f1f1;\n}\n#T_ec2a2_row12_col3 {\n  background-color: #fee0d2;\n  color: #000000;\n}\n#T_ec2a2_row12_col4 {\n  background-color: #b51318;\n  color: #f1f1f1;\n}\n#T_ec2a2_row13_col3 {\n  background-color: #fdd3c1;\n  color: #000000;\n}\n#T_ec2a2_row13_col4 {\n  background-color: #9c0d14;\n  color: #f1f1f1;\n}\n#T_ec2a2_row14_col1 {\n  background-color: #fff2eb;\n  color: #000000;\n}\n#T_ec2a2_row14_col3 {\n  background-color: #fcb99f;\n  color: #000000;\n}\n#T_ec2a2_row15_col0 {\n  background-color: #fee3d7;\n  color: #000000;\n}\n#T_ec2a2_row15_col1 {\n  background-color: #fee5d9;\n  color: #000000;\n}\n#T_ec2a2_row15_col3 {\n  background-color: #fcb499;\n  color: #000000;\n}\n#T_ec2a2_row15_col4 {\n  background-color: #b71319;\n  color: #f1f1f1;\n}\n#T_ec2a2_row16_col3 {\n  background-color: #ffebe2;\n  color: #000000;\n}\n#T_ec2a2_row16_col4 {\n  background-color: #71020e;\n  color: #f1f1f1;\n}\n#T_ec2a2_row17_col3 {\n  background-color: #ffede5;\n  color: #000000;\n}\n#T_ec2a2_row18_col4 {\n  background-color: #bc141a;\n  color: #f1f1f1;\n}\n</style>\n<table id=\"T_ec2a2_\">\n  <thead>\n    <tr>\n      <th class=\"blank level0\" >&nbsp;</th>\n      <th class=\"col_heading level0 col0\" >confirmed</th>\n      <th class=\"col_heading level0 col1\" >recovered</th>\n      <th class=\"col_heading level0 col2\" >death</th>\n      <th class=\"col_heading level0 col3\" >Mortality</th>\n      <th class=\"col_heading level0 col4\" >Recovery</th>\n      <th class=\"col_heading level0 col5\" >Clusters</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th id=\"T_ec2a2_level0_row0\" class=\"row_heading level0 row0\" >Hainan</th>\n      <td id=\"T_ec2a2_row0_col0\" class=\"data row0 col0\" >112860.00</td>\n      <td id=\"T_ec2a2_row0_col1\" class=\"data row0 col1\" >88741.00</td>\n      <td id=\"T_ec2a2_row0_col2\" class=\"data row0 col2\" >3789.00</td>\n      <td id=\"T_ec2a2_row0_col3\" class=\"data row0 col3\" >3.36</td>\n      <td id=\"T_ec2a2_row0_col4\" class=\"data row0 col4\" >78.63</td>\n      <td id=\"T_ec2a2_row0_col5\" class=\"data row0 col5\" >1.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_ec2a2_level0_row1\" class=\"row_heading level0 row1\" >Hubei</th>\n      <td id=\"T_ec2a2_row1_col0\" class=\"data row1 col0\" >43243744.00</td>\n      <td id=\"T_ec2a2_row1_col1\" class=\"data row1 col1\" >33124012.00</td>\n      <td id=\"T_ec2a2_row1_col2\" class=\"data row1 col2\" >2754515.00</td>\n      <td id=\"T_ec2a2_row1_col3\" class=\"data row1 col3\" >6.37</td>\n      <td id=\"T_ec2a2_row1_col4\" class=\"data row1 col4\" >76.60</td>\n      <td id=\"T_ec2a2_row1_col5\" class=\"data row1 col5\" >1.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_ec2a2_level0_row2\" class=\"row_heading level0 row2\" >Yunnan</th>\n      <td id=\"T_ec2a2_row2_col0\" class=\"data row2 col0\" >260015.00</td>\n      <td id=\"T_ec2a2_row2_col1\" class=\"data row2 col1\" >121836.00</td>\n      <td id=\"T_ec2a2_row2_col2\" class=\"data row2 col2\" >1251.00</td>\n      <td id=\"T_ec2a2_row2_col3\" class=\"data row2 col3\" >0.48</td>\n      <td id=\"T_ec2a2_row2_col4\" class=\"data row2 col4\" >46.86</td>\n      <td id=\"T_ec2a2_row2_col5\" class=\"data row2 col5\" >2.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_ec2a2_level0_row3\" class=\"row_heading level0 row3\" >Taiwan</th>\n      <td id=\"T_ec2a2_row3_col0\" class=\"data row3 col0\" >8169.00</td>\n      <td id=\"T_ec2a2_row3_col1\" class=\"data row3 col1\" >1307.00</td>\n      <td id=\"T_ec2a2_row3_col2\" class=\"data row3 col2\" >114.00</td>\n      <td id=\"T_ec2a2_row3_col3\" class=\"data row3 col3\" >1.40</td>\n      <td id=\"T_ec2a2_row3_col4\" class=\"data row3 col4\" >16.00</td>\n      <td id=\"T_ec2a2_row3_col5\" class=\"data row3 col5\" >2.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_ec2a2_level0_row4\" class=\"row_heading level0 row4\" >Anhui</th>\n      <td id=\"T_ec2a2_row4_col0\" class=\"data row4 col0\" >637517.00</td>\n      <td id=\"T_ec2a2_row4_col1\" class=\"data row4 col1\" >524556.00</td>\n      <td id=\"T_ec2a2_row4_col2\" class=\"data row4 col2\" >3803.00</td>\n      <td id=\"T_ec2a2_row4_col3\" class=\"data row4 col3\" >0.60</td>\n      <td id=\"T_ec2a2_row4_col4\" class=\"data row4 col4\" >82.28</td>\n      <td id=\"T_ec2a2_row4_col5\" class=\"data row4 col5\" >0.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_ec2a2_level0_row5\" class=\"row_heading level0 row5\" >Beijing</th>\n      <td id=\"T_ec2a2_row5_col0\" class=\"data row5 col0\" >588739.00</td>\n      <td id=\"T_ec2a2_row5_col1\" class=\"data row5 col1\" >451555.00</td>\n      <td id=\"T_ec2a2_row5_col2\" class=\"data row5 col2\" >5577.00</td>\n      <td id=\"T_ec2a2_row5_col3\" class=\"data row5 col3\" >0.95</td>\n      <td id=\"T_ec2a2_row5_col4\" class=\"data row5 col4\" >76.70</td>\n      <td id=\"T_ec2a2_row5_col5\" class=\"data row5 col5\" >0.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_ec2a2_level0_row6\" class=\"row_heading level0 row6\" >Chongqing</th>\n      <td id=\"T_ec2a2_row6_col0\" class=\"data row6 col0\" >378384.00</td>\n      <td id=\"T_ec2a2_row6_col1\" class=\"data row6 col1\" >307823.00</td>\n      <td id=\"T_ec2a2_row6_col2\" class=\"data row6 col2\" >3809.00</td>\n      <td id=\"T_ec2a2_row6_col3\" class=\"data row6 col3\" >1.01</td>\n      <td id=\"T_ec2a2_row6_col4\" class=\"data row6 col4\" >81.35</td>\n      <td id=\"T_ec2a2_row6_col5\" class=\"data row6 col5\" >0.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_ec2a2_level0_row7\" class=\"row_heading level0 row7\" >Fujian</th>\n      <td id=\"T_ec2a2_row7_col0\" class=\"data row7 col0\" >355425.00</td>\n      <td id=\"T_ec2a2_row7_col1\" class=\"data row7 col1\" >240662.00</td>\n      <td id=\"T_ec2a2_row7_col2\" class=\"data row7 col2\" >625.00</td>\n      <td id=\"T_ec2a2_row7_col3\" class=\"data row7 col3\" >0.18</td>\n      <td id=\"T_ec2a2_row7_col4\" class=\"data row7 col4\" >67.71</td>\n      <td id=\"T_ec2a2_row7_col5\" class=\"data row7 col5\" >0.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_ec2a2_level0_row8\" class=\"row_heading level0 row8\" >Gansu</th>\n      <td id=\"T_ec2a2_row8_col0\" class=\"data row8 col0\" >112355.00</td>\n      <td id=\"T_ec2a2_row8_col1\" class=\"data row8 col1\" >88658.00</td>\n      <td id=\"T_ec2a2_row8_col2\" class=\"data row8 col2\" >1273.00</td>\n      <td id=\"T_ec2a2_row8_col3\" class=\"data row8 col3\" >1.13</td>\n      <td id=\"T_ec2a2_row8_col4\" class=\"data row8 col4\" >78.91</td>\n      <td id=\"T_ec2a2_row8_col5\" class=\"data row8 col5\" >0.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_ec2a2_level0_row9\" class=\"row_heading level0 row9\" >Guangdong</th>\n      <td id=\"T_ec2a2_row9_col0\" class=\"data row9 col0\" >1367847.00</td>\n      <td id=\"T_ec2a2_row9_col1\" class=\"data row9 col1\" >1024876.00</td>\n      <td id=\"T_ec2a2_row9_col2\" class=\"data row9 col2\" >5001.00</td>\n      <td id=\"T_ec2a2_row9_col3\" class=\"data row9 col3\" >0.37</td>\n      <td id=\"T_ec2a2_row9_col4\" class=\"data row9 col4\" >74.93</td>\n      <td id=\"T_ec2a2_row9_col5\" class=\"data row9 col5\" >0.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_ec2a2_level0_row10\" class=\"row_heading level0 row10\" >Guangxi</th>\n      <td id=\"T_ec2a2_row10_col0\" class=\"data row10 col0\" >171744.00</td>\n      <td id=\"T_ec2a2_row10_col1\" class=\"data row10 col1\" >136808.00</td>\n      <td id=\"T_ec2a2_row10_col2\" class=\"data row10 col2\" >1268.00</td>\n      <td id=\"T_ec2a2_row10_col3\" class=\"data row10 col3\" >0.74</td>\n      <td id=\"T_ec2a2_row10_col4\" class=\"data row10 col4\" >79.66</td>\n      <td id=\"T_ec2a2_row10_col5\" class=\"data row10 col5\" >0.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_ec2a2_level0_row11\" class=\"row_heading level0 row11\" >Guizhou</th>\n      <td id=\"T_ec2a2_row11_col0\" class=\"data row11 col0\" >93998.00</td>\n      <td id=\"T_ec2a2_row11_col1\" class=\"data row11 col1\" >76757.00</td>\n      <td id=\"T_ec2a2_row11_col2\" class=\"data row11 col2\" >1267.00</td>\n      <td id=\"T_ec2a2_row11_col3\" class=\"data row11 col3\" >1.35</td>\n      <td id=\"T_ec2a2_row11_col4\" class=\"data row11 col4\" >81.66</td>\n      <td id=\"T_ec2a2_row11_col5\" class=\"data row11 col5\" >0.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_ec2a2_level0_row12\" class=\"row_heading level0 row12\" >Hebei</th>\n      <td id=\"T_ec2a2_row12_col0\" class=\"data row12 col0\" >509851.00</td>\n      <td id=\"T_ec2a2_row12_col1\" class=\"data row12 col1\" >359271.00</td>\n      <td id=\"T_ec2a2_row12_col2\" class=\"data row12 col2\" >4093.00</td>\n      <td id=\"T_ec2a2_row12_col3\" class=\"data row12 col3\" >0.80</td>\n      <td id=\"T_ec2a2_row12_col4\" class=\"data row12 col4\" >70.47</td>\n      <td id=\"T_ec2a2_row12_col5\" class=\"data row12 col5\" >0.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_ec2a2_level0_row13\" class=\"row_heading level0 row13\" >Heilongjiang</th>\n      <td id=\"T_ec2a2_row13_col0\" class=\"data row13 col0\" >771143.00</td>\n      <td id=\"T_ec2a2_row13_col1\" class=\"data row13 col1\" >579102.00</td>\n      <td id=\"T_ec2a2_row13_col2\" class=\"data row13 col2\" >8254.00</td>\n      <td id=\"T_ec2a2_row13_col3\" class=\"data row13 col3\" >1.07</td>\n      <td id=\"T_ec2a2_row13_col4\" class=\"data row13 col4\" >75.10</td>\n      <td id=\"T_ec2a2_row13_col5\" class=\"data row13 col5\" >0.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_ec2a2_level0_row14\" class=\"row_heading level0 row14\" >Henan</th>\n      <td id=\"T_ec2a2_row14_col0\" class=\"data row14 col0\" >849859.00</td>\n      <td id=\"T_ec2a2_row14_col1\" class=\"data row14 col1\" >675238.00</td>\n      <td id=\"T_ec2a2_row14_col2\" class=\"data row14 col2\" >13880.00</td>\n      <td id=\"T_ec2a2_row14_col3\" class=\"data row14 col3\" >1.63</td>\n      <td id=\"T_ec2a2_row14_col4\" class=\"data row14 col4\" >79.45</td>\n      <td id=\"T_ec2a2_row14_col5\" class=\"data row14 col5\" >0.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_ec2a2_level0_row15\" class=\"row_heading level0 row15\" >Hong Kong</th>\n      <td id=\"T_ec2a2_row15_col0\" class=\"data row15 col0\" >4587618.00</td>\n      <td id=\"T_ec2a2_row15_col1\" class=\"data row15 col1\" >3213515.00</td>\n      <td id=\"T_ec2a2_row15_col2\" class=\"data row15 col2\" >79233.00</td>\n      <td id=\"T_ec2a2_row15_col3\" class=\"data row15 col3\" >1.73</td>\n      <td id=\"T_ec2a2_row15_col4\" class=\"data row15 col4\" >70.05</td>\n      <td id=\"T_ec2a2_row15_col5\" class=\"data row15 col5\" >0.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_ec2a2_level0_row16\" class=\"row_heading level0 row16\" >Hunan</th>\n      <td id=\"T_ec2a2_row16_col0\" class=\"data row16 col0\" >674506.00</td>\n      <td id=\"T_ec2a2_row16_col1\" class=\"data row16 col1\" >545312.00</td>\n      <td id=\"T_ec2a2_row16_col2\" class=\"data row16 col2\" >2526.00</td>\n      <td id=\"T_ec2a2_row16_col3\" class=\"data row16 col3\" >0.37</td>\n      <td id=\"T_ec2a2_row16_col4\" class=\"data row16 col4\" >80.85</td>\n      <td id=\"T_ec2a2_row16_col5\" class=\"data row16 col5\" >0.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_ec2a2_level0_row17\" class=\"row_heading level0 row17\" >Inner Mongolia</th>\n      <td id=\"T_ec2a2_row17_col0\" class=\"data row17 col0\" >198128.00</td>\n      <td id=\"T_ec2a2_row17_col1\" class=\"data row17 col1\" >148617.00</td>\n      <td id=\"T_ec2a2_row17_col2\" class=\"data row17 col2\" >613.00</td>\n      <td id=\"T_ec2a2_row17_col3\" class=\"data row17 col3\" >0.31</td>\n      <td id=\"T_ec2a2_row17_col4\" class=\"data row17 col4\" >75.01</td>\n      <td id=\"T_ec2a2_row17_col5\" class=\"data row17 col5\" >0.00</td>\n    </tr>\n    <tr>\n      <th id=\"T_ec2a2_level0_row18\" class=\"row_heading level0 row18\" >Jiangsu</th>\n      <td id=\"T_ec2a2_row18_col0\" class=\"data row18 col0\" >522012.00</td>\n      <td id=\"T_ec2a2_row18_col1\" class=\"data row18 col1\" >360083.00</td>\n      <td id=\"T_ec2a2_row18_col2\" class=\"data row18 col2\" >0.00</td>\n      <td id=\"T_ec2a2_row18_col3\" class=\"data row18 col3\" >0.00</td>\n      <td id=\"T_ec2a2_row18_col4\" class=\"data row18 col4\" >68.98</td>\n      <td id=\"T_ec2a2_row18_col5\" class=\"data row18 col5\" >0.00</td>\n    </tr>\n  </tbody>\n</table>\n"
     },
     "metadata": {},
     "execution_count": 6
    }
   ],
   "source": [
    "clf_final = KMeans(n_clusters=3, init='k-means++', random_state=6)\n",
    "# clf_final = KMeans(n_clusters=3, init='k-means++', random_state=6)\n",
    "clf_final.fit(X)\n",
    "\n",
    "# 分类\n",
    "countrywise[\"Clusters\"] = clf_final.predict(X)\n",
    "\n",
    "cluster_summary = pd.concat([countrywise[countrywise[\"Clusters\"] == 1].head(15),\n",
    "                            countrywise[countrywise[\"Clusters\"] == 2].head(15),\n",
    "                            countrywise[countrywise[\"Clusters\"] == 0].head(15)])\n",
    "cluster_summary.style.background_gradient(cmap='Reds').format(\"{:.2f}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.9.7 64-bit ('software': virtualenv)"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "3.9.7"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "source": [],
    "metadata": {
     "collapsed": false
    }
   }
  },
  "interpreter": {
   "hash": "c72f12f90170356fe3abc9748a888f8a0f253bf3dee690070f84c0db82f6a8c0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}